<?php

namespace App\Jobs;

use App\Models\Custom;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\CustomThrough as Model;

class GetCustomJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    private $data;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data=$data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $model=new Model();
        $through=$model->where($this->data)->first();
        if (!$through){
            $this->data["through"]=1;
            $model->create($this->data);
        }else{
            Model::find($through["id"])->update(["through"=>$through["through"]+1]);
        }
    }
}
